package com.example.content_provider_client;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.content_provider_client.utils.PermissionUtil;
import com.example.content_provider_client.utils.ToastHelper;

public class PermissionLazyActivity extends AppCompatActivity implements View.OnClickListener {

    // 通讯录权限列表：字符串数组{ 读权限，写权限 }
    private static final String[] PERMISSIONS_CONTACTS = new String[]{
            Manifest.permission.READ_CONTACTS,  // 读权限
            Manifest.permission.WRITE_CONTACTS,  // 写权限
    };
    // 短信权限列表：字符串数组{ 读权限，写权限 }
    private static final String[] PERMISSIONS_SMS = new String[]{
            Manifest.permission.READ_SMS,  // 读权限
            Manifest.permission.SEND_SMS,  // 写权限
    };

    // 自定义的权限代号 - 联系人
    private static final int REQUEST_CODE_CONTACTS = 1;
    // 自定义的权限代号 - 短信
    private static final int REQUEST_CODE_SMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_lazy);

        findViewById(R.id.btn_contacts).setOnClickListener(this);
        findViewById(R.id.btn_sms).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        // 检查App是否开启了权限
        // 请求系统弹窗，以便用户选择是否开启权限
        // 检查App是否开启了权限


        if (R.id.btn_contacts == id) {
            PermissionUtil.checkPermission(this, PERMISSIONS_CONTACTS, REQUEST_CODE_CONTACTS);

        } else if (R.id.btn_sms == id) {
            PermissionUtil.checkPermission(this, PERMISSIONS_SMS, REQUEST_CODE_SMS);
        }
    }

    //

    /**
     * 权限请求弹窗，用户点击同意还是拒绝后的回调
     *
     * @param requestCode  自定义的权限代号 ,用于区分当前请求的是什么权限
     * @param permissions  The requested permissions. Never null.
     * @param grantResults 权限数组中对应结果，
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // 判断当前是通讯录权限弹窗还是短信权限弹窗
        switch (requestCode) {
            case REQUEST_CODE_CONTACTS:
                if (PermissionUtil.checkGrant(grantResults)) {
                    ToastHelper.show(this, "获取通讯录权限-成功");
                    Log.d("zhoutao", "获取通讯录权限-成功");
                } else {
                    ToastHelper.show(this, "获取通讯录权限-失败");
                    Log.d("zhoutao", "获取通讯录权限-失败");
                    jumpToSettings();
                }
                break;

            case REQUEST_CODE_SMS:
                if (PermissionUtil.checkGrant(grantResults)) {
                    ToastHelper.show(this, "获取短信权限-成功");
                    Log.d("zhoutao", "获取短信权限-成功");
                } else {
                    ToastHelper.show(this, "获取短信权限-失败");
                    Log.d("zhoutao", "获取短信权限-失败");
                    jumpToSettings();
                }
                break;
        }
    }

    private void jumpToSettings() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", getPackageName(), null));
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}